using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

/**
    feature：software installer by robotwu
    author: robotwu
    date: 2018年2月27日
    
*/

namespace Installer
{

    class Program
    {


        public const String softPath = @"\\10.196.129.10\Information Technology\工具";

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        private extern static bool ShowWindow(IntPtr hWnd, Int16 nCmdShow);


        static void Main(string[] args)
        {
            Console.Title = "software installer by robotwu";
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            IntPtr hWnd = FindWindow("ConsoleWindowClass", Console.Title);
            Int16 SW_SHOWMAXIMIZED = 3;
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, SW_SHOWMAXIMIZED);
            }
            try
            {
                DirectoryInfo folder = new DirectoryInfo(softPath);
                FileInfo[] files = folder.GetFiles("*.exe");
                Console.WriteLine("sequence:\t software name\t   Length\n");
                for (int i = 0; i < files.Length; i++)
                {
                    Console.WriteLine("{0}:\t{1}\t{2:f} MB", i, files[i], files[i].Length / 1024.00 / 1024.00);
                }
                Console.Write("\nChoice a sequence！：");
                int input = Int32.Parse(Console.ReadLine());
                Program pg = new Program();
                String sourceFile = softPath + "\\" + files[input].ToString();
                String targetFile = Environment.GetEnvironmentVariable("Temp") + "\\" + files[input].ToString();
                if( ApiCopyFile.DoCopy(sourceFile, Environment.GetEnvironmentVariable("Temp")))
                {
                    pg.Launch(targetFile);
                }
                
            }
            catch (System.FormatException)
            {
                return;
            }
            catch (System.IndexOutOfRangeException)
            {
                MessageBox.Show("序号选择错误", "提示");
                Console.Clear();
                Main(null);
                return;
            }
            catch (System.IO.IOException e)
            {
                MessageBox.Show(e.Message, "提示");

                return;
            }
            catch (Exception)
            {
                MessageBox.Show("程序未知错误，请联系RobotWu!", "提示");
                return;
            }
        }


        public void Launch(String fileName)
        {
            try
            {
                const String domain = "";
                const String userName = "";
                const String password = "";
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = fileName;
                psi.UseShellExecute = false;

                System.Security.SecureString pwd = new System.Security.SecureString();
                psi.Domain = domain;
                psi.UserName = userName;
                foreach (char c in password)
                {
                    pwd.AppendChar(c);
                }

                psi.Password = pwd;
                Process p = new Process();
                p.StartInfo = psi;

                p.Start();
            }
            catch (System.ComponentModel.Win32Exception e)
            {
                
                MessageBox.Show(e.Message, "提示");
                
            }
        }
    }
}